﻿using ToqueDeDarsena;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.SqlClient;
using System.Data;
using Model;
using WebMatrix.WebData;




namespace ToqueDeDarsena.Controllers
{
    public class DarsenaController : Controller
    {
      
        private DataContext dataContext = new DataContext();



        public ActionResult Index()
        {
            var darsena = dataContext.darsenas;
            return View(model: darsena);
        }

        public ActionResult BorrarDarsena(int id)
        {
            var darsena = dataContext.darsenas.Find(id);
            dataContext.darsenas.Remove(darsena);
            dataContext.SaveChanges();

            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult EditarDarsena(int id)
        {
            var darsena = dataContext.darsenas.Find(id);
            return View(model: darsena);
        }

        [HttpPost]
        public ActionResult EditarDarsena(Darsena darsena)
        {
          // var darsena = dataContext.darsenas.Find(id);
         

            if (darsena.nroDarsena < 10 && darsena.nroDarsena > 1 && darsena.interno>0)
            {
                //darsena.interno = interno;
                //darsena.nroDarsena = nroDarsena;
                // darsena.hora = DateTime.Now;


                dataContext.SaveChanges();
                return RedirectToAction("Index", "Darsena");

            }

            ViewBag.error = true;
            darsena.empresa = new Empresa();
            darsena.localidad = new Localidad();
            return View(model: darsena);


        }

        [HttpGet]
        public ActionResult AgregarDarsena()
        {
            Darsena darsena = new Darsena();
            return View(model:darsena);
        }

        [HttpPost]
        public ActionResult AgregarDarsena(Darsena darsena, int codigoLocalidad, int codigoEmpresa)
        {
                     


            if (ModelState.IsValid && darsena.interno != 0 && darsena.interno > 0 && darsena.nroDarsena > 0 && darsena.nroDarsena <= 10)
            {
                
              


                var localidad = dataContext.buscarLocalidad(codigoLocalidad);
                if (localidad == null)
                {
                    
                    ViewBag.ErrorLocalidad = true;
                    return this.View(model:darsena); 
                
                }

                darsena.localidad = localidad;
                //darsena.interno = interno;
                //darsena.nroDarsena = nroDarsena;

                var empresa = dataContext.buscarEmpresa(codigoEmpresa);
                if (empresa == null)
                {

                    ViewBag.ErrorEmpresa = true;
                    return this.View();

                }
                darsena.empresa = empresa;
               
                darsena.hora = DateTime.Now;

                // Lo guardo en la db
                dataContext.darsenas.Add(darsena);
                dataContext.SaveChanges();
                return RedirectToAction("Index", "Darsena");
            }

            ViewBag.Error = true;
            return this.View(model: darsena);
        }




        public ActionResult consultarEmpresa(int id)
        {

            var empresa = dataContext.buscarEmpresa(id);
            return Json(empresa, JsonRequestBehavior.AllowGet);



        }

        public ActionResult consultarLocalidad(int id)
        {

            var localidad = dataContext.buscarLocalidad(id);
            return Json(localidad, JsonRequestBehavior.AllowGet);



        }


    }
}
